<template>
  <div class="medicalPerson">
    <div class="medicalPerson-header">
      <el-row>
        <el-col :span="1">
          <el-button
            type="primary"
            v-btnpermission="'btn-medicaladd'"
            @click="addDialogVisible = true"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1">
          <!-- 导入 -->
          <!-- <el-upload
            class="upload"
            :multiple="false"
            :show-file-list="false"
            accept="csv, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            :http-request="toLead"
          >
            <template #trigger>
              <el-button v-btnpermission="'btn-medicalload'" type="primary"
                >导入</el-button
              >
            </template>
          </el-upload> -->
        </el-col>
      </el-row>
      <el-card class="drug-head-contatiner-title2">
        <el-form :inline="true" :model="formSearch" class="demo-form-inline">
          <el-form-item label="所属城市" prop="citycode">
            <el-tree-select
              v-model="formSearch.citycode"
              :data="cityTreeOptionSearch"
              :props="defaultProps"
              node-key="Code"
              :check-strictly="true"
              :render-after-expand="false"
              clearable
              filterable
            />
          </el-form-item>
          <el-form-item label="场馆">
            <el-select
              v-model="formSearch.activityvenuescode"
              placeholder="--选择--"
              clearable
              filterable
            >
              <el-option
                v-for="item in activityVenuesOptionSearch"
                :key="item.Code"
                :label="item.CHIDescription"
                :value="item.Code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="医疗点">
            <el-select
              v-model="formSearch.medicalPoint"
              placeholder="--选择--"
              clearable
              filterable
            >
              <el-option
                v-for="item in medicalPointOptionSearch"
                :key="item.Code"
                :label="item.Name"
                :value="item.Code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="所属车辆">
            <el-select
              v-model="formSearch.ambulanceCode"
              placeholder="--选择--"
              clearable
              filterable
            >
              <el-option
                v-for="item in ambulanceNameOption"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model="formSearch.Name" />
          </el-form-item>
          <el-form-item label="角色">
            <el-select
              v-model="formSearch.Type"
              placeholder="--请选择--"
              filterable
              clearable
            >
              <el-option
                v-for="item in rolesOption"
                :key="item.Code"
                :label="item.Name"
                :value="item.Code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="工号">
            <el-input v-model="formSearch.Code" />
          </el-form-item>
          <el-form-item label="保障类型">
            <el-select
              v-model="formSearch.GuaranteeType"
              placeholder="--请选择--"
              clearable
            >
              <el-option
                v-for="item in GuaranteeTypeOption"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="onSubmit">搜索</el-button>
            <el-button @click="onReset">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <div class="medicalPerson-list">
      <el-row>
        <el-col>
          <!-- 角色列表 -->
          <el-table :data="medicalPersonList" border stripe>
            <!-- 索引列 -->
            <el-table-column
              type="index"
              label="#"
              :index="indexMethod"
            ></el-table-column>
            <el-table-column prop="WorkCode" label="工号"></el-table-column>
            <el-table-column prop="Name" label="姓名"></el-table-column>
            <el-table-column
              prop="PersonTypeName"
              label="角色"
            ></el-table-column>
            <el-table-column
              prop="Telephone"
              label="联系方式"
            ></el-table-column>
            <el-table-column prop="VenuesName" label="场馆"></el-table-column>
            <el-table-column
              prop="MedicalPointName"
              label="绑定车辆/医疗点"
            ></el-table-column>
            <el-table-column
              prop="GuaranteeName"
              label="保障类型"
            ></el-table-column>
            <el-table-column prop="CityName" label="所属城市"></el-table-column>
            <el-table-column prop="IsValid" label="是否有效">
              <template #default="scope">
                <el-switch
                  v-model="scope.row.IsValid"
                  :active-value="1"
                  :inactive-value="0"
                  disabled
                />
              </template>
            </el-table-column>
            <el-table-column label="操作" width="170">
              <template #default="scope">
                <el-button
                  size="small"
                  type="primary"
                  v-btnpermission="'btn-medicalupdate'"
                  :icon="Edit"
                  @click="showEditDialog(scope.row)"
                  >编辑</el-button
                >
                <el-button
                  size="small"
                  type="danger"
                  v-btnpermission="'btn-medicaldelete'"
                  @click="rowDelete(scope.row.Code)"
                  :icon="Delete"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12" :offset="8">
          <el-pagination
            background
            layout="total,prev, pager, next"
            v-model:total="formSearch.total"
            v-model:page-size="formSearch.pageSize"
            v-model:current-page="formSearch.page"
            @current-change="handleCurrentChange"
          />
        </el-col>
      </el-row>
      <el-dialog
        title="新增人员"
        v-model="addDialogVisible"
        width="40%"
        @close="addDislogClosed"
      >
        <el-form
          :model="addDialogForm"
          :rules="medicalPersonFormRulesMixin.addFormRules"
          ref="addDialogRef"
          label-width="100px"
          inline
        >
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="工号" prop="WorkCode">
                <el-input v-model="addDialogForm.WorkCode" /> </el-form-item
            ></el-col>
            <el-col :span="10">
              <el-form-item label="姓名" prop="Name">
                <el-input v-model="addDialogForm.Name" /> </el-form-item
            ></el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="密码" prop="PassWord">
                <el-input v-model="addDialogForm.PassWord" /> </el-form-item
            ></el-col>
            <el-col :span="10">
              <el-form-item label="类型" prop="Type">
                <el-select
                  v-model="addDialogForm.Type"
                  placeholder="--请选择--"
                >
                  <el-option
                    v-for="item in rolesOption"
                    :key="item.Code"
                    :label="item.Name"
                    :value="item.Code"
                  />
                </el-select> </el-form-item
            ></el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="年龄" prop="Age">
                <el-input v-model="addDialogForm.Age" /> </el-form-item
            ></el-col>
            <el-col :span="10">
              <el-form-item prop="Telephone" label="手机号">
                <el-input v-model="addDialogForm.Telephone" /> </el-form-item
            ></el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="所属活动" prop="ActivityCode">
                <el-select
                  v-model="addDialogForm.ActivityCode"
                  placeholder="--请选择--"
                  filterable
                >
                  <el-option
                    v-for="item in activityOption"
                    :key="item.Code"
                    :label="item.Name"
                    :value="item.Code"
                  />
                </el-select> </el-form-item
            ></el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="所属城市" prop="CityCode">
                <el-tree-select
                  v-model="addDialogForm.CityCode"
                  :data="cityTreeOption"
                  :props="defaultProps"
                  :render-after-expand="false"
                  node-key="Code"
                  check-strictly
                  @change="cs11"
                /> </el-form-item
            ></el-col>
            <el-col :span="14">
              <el-form-item label="所属场馆" prop="VenuesCode">
                <el-select
                  multiple
                  collapse-tags
                  collapse-tags-tooltip
                  v-model="addDialogForm.VenuesCode"
                  placeholder="--请选择--"
                  filterable
                  clearable
                >
                  <el-option
                    v-for="item in venuesOption"
                    :key="item.Code"
                    :label="item.CHIDescription"
                    :value="item.Code"
                  />
                </el-select> </el-form-item
            ></el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="保障类型" prop="GuaranteeType">
                <el-select
                  v-model="addDialogForm.GuaranteeType"
                  placeholder="--请选择--"
                  clearable
                >
                  <el-option
                    v-for="item in GuaranteeTypeOption"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="性别" prop="Sex">
                <el-select v-model="addDialogForm.Sex" placeholder="--请选择--">
                  <el-option
                    v-for="item in sexOption"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select> </el-form-item
            ></el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="医疗点" prop="MedicalPointCode">
                <el-select
                  v-model="addDialogForm.MedicalPointCode"
                  placeholder="--请选择--"
                  clearable
                  filterable
                >
                  <el-option
                    v-for="item in medicalPointOption"
                    :key="item.Code"
                    :label="item.Name"
                    :value="item.Code"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="标题" prop="Title">
                <el-input v-model="addDialogForm.Title" /> </el-form-item
            ></el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="14">
              <el-form-item label="外部标识" prop="ExternalMarker">
                <el-input
                  v-model="addDialogForm.ExternalMarker"
                /> </el-form-item
            ></el-col>
            <el-col :span="8">
              <el-form-item label="是否有效" prop="IsValid">
                <el-checkbox
                  v-model="addDialogForm.IsValid"
                  :true-label="1"
                  :false-label="0"
                /> </el-form-item
            ></el-col>
          </el-row>
          <el-row>
            <el-col :span="10">
              <el-form-item label="设备ID" prop="EquipmentId">
                <el-input v-model="addDialogForm.EquipmentId" />
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="MED注册码" prop="DoctRegisNo">
                <el-input v-model="addDialogForm.DoctRegisNo" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="预计下班时间" prop="EstimatedClosingTime">
                <el-date-picker
                  v-model="addDialogForm.EstimatedClosingTime"
                  type="datetime"
                  placeholder="--请选择--"
                  value-format="YYYY-MM-DD HH:mm:ss"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <template v-slot:footer>
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addFormSubmit">确 定</el-button>
        </template>
      </el-dialog>
      <el-dialog
        title="编辑人员"
        v-model="editDialogVisible"
        @close="editDislogClosed"
        width="40%"
      >
        <el-form
          :model="editDialogForm"
          :rules="medicalPersonFormRulesMixin.editFormRules"
          ref="editDialogRef"
          label-width="100px"
          inline
        >
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="工号" prop="WorkCode">
                <el-input v-model="editDialogForm.WorkCode" /> </el-form-item
            ></el-col>
            <el-col :span="10">
              <el-form-item label="姓名" prop="Name">
                <el-input v-model="editDialogForm.Name" /> </el-form-item
            ></el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="性别" prop="Sex">
                <el-select
                  v-model="editDialogForm.Sex"
                  placeholder="--请选择--"
                >
                  <el-option
                    v-for="item in sexOption"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select> </el-form-item
            ></el-col>
            <el-col :span="10">
              <el-form-item label="年龄" prop="Age">
                <el-input v-model="editDialogForm.Age" /> </el-form-item
            ></el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="密码重置" prop="">
                <el-switch v-model="editDialogForm.reSetPassWord" />
                <!-- <el-input v-model="editDialogForm.PassWord" /> -->
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="类型" prop="Type">
                <el-select
                  v-model="editDialogForm.Type"
                  placeholder="--请选择--"
                >
                  <el-option
                    v-for="item in rolesOption"
                    :key="item.Code"
                    :label="item.Name"
                    :value="item.Code"
                  />
                </el-select> </el-form-item
            ></el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="所属活动" prop="ActivityCode">
                <el-select
                  v-model="editDialogForm.ActivityCode"
                  placeholder="--请选择--"
                  filterable
                >
                  <el-option
                    v-for="item in activityOption"
                    :key="item.Code"
                    :label="item.Name"
                    :value="item.Code"
                  />
                </el-select> </el-form-item
            ></el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="所属城市" prop="CityCode">
                <el-tree-select
                  v-model="editDialogForm.CityCode"
                  :data="cityTreeOptionUpdate"
                  :props="defaultProps"
                  :render-after-expand="false"
                  node-key="Code"
                  @change="cs11Update"
                  check-strictly
                /> </el-form-item
            ></el-col>
            <el-col :span="10">
              <el-form-item label="所属场馆" prop="VenuesCode">
                <el-select
                  multiple
                  collapse-tags
                  collapse-tags-tooltip
                  v-model="editDialogForm.VenuesCode"
                  placeholder="--请选择--"
                  filterable
                  clearable
                >
                  <el-option
                    v-for="item in venuesOptionUpdate"
                    :key="item.Code"
                    :label="item.CHIDescription"
                    :value="item.Code"
                  />
                </el-select> </el-form-item
            ></el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="保障类型" prop="GuaranteeType">
                <el-select
                  v-model="editDialogForm.GuaranteeType"
                  placeholder="--请选择--"
                  clearable
                >
                  <el-option
                    v-for="item in GuaranteeTypeOption"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item prop="Telephone" label="手机号">
                <el-input v-model="editDialogForm.Telephone" /> </el-form-item
            ></el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="  " prop="MedicalPointCode">
                <el-select
                  ref="MedicalPointRef"
                  v-model="editDialogForm.MedicalPointCode"
                  placeholder="--请选择--"
                  clearable
                >
                  <el-option
                    v-for="item in medicalPointOptionUpdate"
                    :key="item.Code"
                    :label="item.Name"
                    :value="item.Code"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="职称" prop="Title">
                <el-input v-model="editDialogForm.Title" /> </el-form-item
            ></el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="14">
              <el-form-item label="外部标识" prop="ExternalMarker">
                <el-input
                  v-model="editDialogForm.ExternalMarker"
                /> </el-form-item
            ></el-col>
            <el-col :span="8">
              <el-form-item label="是否有效" prop="IsValid">
                <el-checkbox
                  v-model="editDialogForm.IsValid"
                  :true-label="1"
                  :false-label="0"
                /> </el-form-item
            ></el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="设备ID" prop="EquipmentId">
                <el-input v-model="editDialogForm.EquipmentId" />
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="MED注册码" prop="DoctRegisNo">
                <el-input v-model="editDialogForm.DoctRegisNo" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="预计下班时间" prop="EstimatedClosingTime">
                <el-date-picker
                  v-model="editDialogForm.EstimatedClosingTime"
                  type="datetime"
                  placeholder="--请选择--"
                  value-format="YYYY-MM-DD HH:mm:ss"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <template v-slot:footer>
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editFormSubmit">确 定</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup>
import { medicalPersonFormRulesMixin } from '@/utils/mixin.js';
import { importExcel } from '@/utils/excel';
import {
  getMedicalPersonList,
  addMedicalPerson,
  editMedicalPerson,
  delMedicalPerson,
  addMedicalPersonList,
} from '@/network/medicalPerson.js';
import { ref, reactive, watch, onMounted, toRaw } from 'vue';
import {
  getDicByDictionary,
  getActivityVenues,
  getCityTree,
  getRoleDicByTableName,
  getVenuesListByCityCode,
  getMedicPointDic,
  getDicVehicle,
  getMedicPointDicNormal,
  getDicByTableName,
} from '@/network/dictionary.js';
import { ElMessage, ElMessageBox } from 'element-plus';
import { Edit, Delete } from '@element-plus/icons-vue';
//分页索引
const indexMethod = (index) => {
  return index + (formSearch.page - 1) * formSearch.pageSize + 1;
};
// 搜索条件
const formSearch = reactive({
  ambulanceCode: null,
  activityvenuescode: '',
  citycode: '',
  medicalPoint: null,
  Name: '',
  Type: '',
  Code: '',
  GuaranteeType: '',
  total: 0,
  page: 1,
  pageSize: 10,
});
//初始化
let medicalPersonList = ref(null);

const initData = async () => {
  let params = {
    Name: formSearch.Name,
    Type: formSearch.Type,
    Code: formSearch.Code,
    GuaranteeType: formSearch.GuaranteeType,
    page: formSearch.page,
    pageSize: formSearch.pageSize,
    ambulanceCode: formSearch.ambulanceCode,
    medicalPoint: formSearch.medicalPoint,
    activityvenuescode: formSearch.activityvenuescode,
    citycode: formSearch.citycode,
  };
  await getMedicalPersonList(params).then((res) => {
    medicalPersonList.value = res.data;
    formSearch.total = res.total;
  });
};
//初始化字典
const activityOption = ref([]);
const GuaranteeTypeOption = ref([]);
const venuesOption = ref([]);
const venuesOptionUpdate = ref([]);
const rolesOption = ref([]);
const cityTreeOption = ref([]);
const cityTreeOptionUpdate = ref([]);
const medicalPointOption = ref([]);
const ambulanceNameOption = ref([]);
const medicalPointOptionUpdate = ref([]);
const MedicalPointRef = ref(null);
//查询里的联动
const cityTreeOptionSearch = ref([]);
const activityVenuesOptionSearch = ref([]);
const medicalPointOptionSearch = ref([]);
let medicalPointOptionArray = [];
let venuesOptionArray = [];
let ambulanceOptionArray = [];
const defaultProps = {
  children: 'Children',
  label: 'Name',
  value: 'Code',
};
const sexOption = [
  {
    value: '男',
    label: '男',
  },
  {
    value: '女',
    label: '女',
  },
];
// 联动---保障类型改变
const cs11ChangeAdd = async (val, citycode, venues, target) => {
  await getMedicPointDic({
    guarantee: val,
    code: citycode,
    venues: venues?.toString(),
  }).then((result) => {
    medicalPointOption.value = result;
    // target = result;
  });
};
const cs11ChangeUpdate = async (val, citycode, venues, target) => {
  await getMedicPointDic({
    guarantee: val,
    code: citycode,
    venues: venues?.toString(),
  }).then((result) => {
    medicalPointOptionUpdate.value = result;
    // target = result;
  });
};
const initDict = async () => {
  await getDicByTableName({ tableName: 'tactivity' }).then((res) => {
    activityOption.value = res;
  });
  await getDicByDictionary({ type: 'GuaranteeType' }).then((res) => {
    GuaranteeTypeOption.value = res;
  });
  //获取城市
  await getCityTree().then((res) => {
    cityTreeOption.value = res;
    cityTreeOptionUpdate.value = res;
    cityTreeOptionSearch.value = res;
  });
  await getActivityVenues().then((res) => {
    venuesOption.value = res;
    venuesOptionUpdate.value = res;
    activityVenuesOptionSearch.value = res;
    venuesOptionArray = res; //缓存初始值
  });
  //医疗点
  //获取实际标识
  await getDicVehicle().then((res) => {
    ambulanceNameOption.value = res;
    ambulanceOptionArray = res;
  });
  await getMedicPointDicNormal().then((res) => {
    medicalPointOption.value = res;
    medicalPointOptionSearch.value = res;
    medicalPointOptionArray = res;
  });
  await getRoleDicByTableName({ tableName: 'trole' }).then((res) => {
    rolesOption.value = res;
  });
  watch(
    [
      () => addDialogForm.value.GuaranteeType,
      () => addDialogForm.value.CityCode,
      () => addDialogForm.value.VenuesCode,
    ],
    (newValue, oldValue) => {
      //联动---医疗点更改
      addDialogForm.value.MedicalPointCode = '';
      cs11ChangeAdd(
        newValue[0],
        newValue[1],
        newValue[2],
        medicalPointOption.value,
      );
    },
    { deep: true },
  );
  //监听城市的选择
  watch(
    [
      () => editDialogForm.value.GuaranteeType,
      () => editDialogForm.value.CityCode,
      () => editDialogForm.value.VenuesCode,
    ],
    (newValue, oldValue) => {
      //联动---医疗点更改
      editDialogForm.value.MedicalPointCode = null;
      cs11ChangeUpdate(
        newValue[0],
        newValue[1],
        newValue[2],
        medicalPointOptionUpdate,
      );
    },
    { deep: true },
  );
  //监听watch
  watch(
    [() => formSearch.citycode, () => formSearch.activityvenuescode],
    (newValue, oldValue) => {
      // 城市变更--场馆变更--车辆变更--理疗店变更
      if (newValue[0] != oldValue[0]) {
        if (!newValue[0] || newValue[0] == 1) {
          activityVenuesOptionSearch.value = venuesOptionArray;
          medicalPointOptionSearch.value = medicalPointOptionArray;
          ambulanceNameOption.value = ambulanceOptionArray;
        } else {
          formSearch.activityvenuescode = '';
          formSearch.medicalPoint = '';
          formSearch.ambulanceCode = '';
          //车辆和医疗点、场馆都重新赋值
          activityVenuesOptionSearch.value = venuesOptionArray.filter(
            (it) => it.CityCode == newValue[0],
          );
          medicalPointOptionSearch.value = medicalPointOptionArray.filter(
            (it) => it.CityCode == newValue[0],
          );
          ambulanceNameOption.value = ambulanceOptionArray.filter(
            (it) => it.CityCode == newValue[0],
          );
        }
      }
      // 场馆变更   城市不变   车辆、医疗点变
      if (newValue[1] != oldValue[1]) {
        if (!newValue[1]) {
          if (newValue[0] && newValue[0] != 1) {
            medicalPointOptionSearch.value = medicalPointOptionArray.filter(
              (it) => it.CityCode == newValue[0],
            );
            ambulanceNameOption.value = ambulanceOptionArray.filter(
              (it) => it.CityCode == newValue[0],
            );
          } else {
            //医疗点重置
            medicalPointOptionSearch.value = medicalPointOptionArray;
            ambulanceNameOption.value = ambulanceOptionArray;
          }
        } else {
          formSearch.medicalPoint = '';
          formSearch.ambulanceCode = '';
          medicalPointOptionSearch.value = medicalPointOptionArray.filter(
            (it) => it.VenuesCode == newValue[1],
          );
          ambulanceNameOption.value = ambulanceOptionArray.filter(
            (it) => it.VenuesCode == newValue[1],
          );
        }
      }
    },
    {
      deep: true,
    },
  );
};
onMounted(() => {
  initDict().then(() => {
    initData();
  });
});
const cs11 = async (val) => {
  await getVenuesListByCityCode({ code: val }).then((res) => {
    addDialogForm.value.VenuesCode = null;
    venuesOption.value = res;
  });
};
const cs11Update = async (val) => {
  await getVenuesListByCityCode({ code: val }).then((res) => {
    editDialogForm.value.VenuesCode = null;
    venuesOptionUpdate.value = res;
  });
};
//新增
const addDialogVisible = ref(false);
const addDialogRef = ref(null);
let addDialogForm = ref({
  Code: 0,
  WorkCode: '',
  Name: '',
  PassWord: '',
  Type: '',
  ActivityCode: null,
  CityCode: null,
  Telephone: '',
  Sex: '',
  Age: null,
  GuaranteeType: '',
  VenuesCode: null,
  ExternalMarker: null,
  Title: '',
  MedicalPointCode: null,
  IsValid: 1,
  EquipmentId: '',
  EstimatedClosingTime: null,
  DoctRegisNo: null,
  FailLogins: 0,
});
const addFormSubmit = async () => {
  await addDialogRef.value.validate(async (valid) => {
    if (valid) {
      addDialogForm.value.VenuesCode =
        addDialogForm.value.VenuesCode?.toString();
      await addMedicalPerson(addDialogForm.value)
        .then(() => {
          addDialogVisible.value = false;
          initData();
          initDict();
          ElMessage.success('新增成功');
        })
        .catch(() => {
          ElMessage.error('新增失败');
        });
    } else {
      ElMessage.error('请按要求填写');
      return false;
    }
  });
};
const addDislogClosed = () => {
  addDialogRef.value.resetFields();
  addDialogRef.value.clearValidate();
};
const editDislogClosed = () => {
  editDialogRef.value.resetFields();
  editDialogRef.value.clearValidate();
};
//编辑
const editDialogVisible = ref(false);
const editDialogRef = ref(null);
let editDialogForm = ref({
  Code: 0,
  WorkCode: '',
  Name: '',
  PassWord: '',
  reSetPassWord: false,
  Type: '',
  CityCode: 0,
  Telephone: '',
  ActivityCode: null,
  Sex: '',
  Age: null,
  GuaranteeType: '',
  VenuesCode: null,
  ExternalMarker: null,
  Title: '',
  MedicalPointCode: null,
  MedicalPointName: '',
  IsValid: 1,
  EquipmentId: '',
  EstimatedClosingTime: '',
  DoctRegisNo: null,
});

const showEditDialog = (row) => {
  //监听场馆的选择
  editDialogVisible.value = true;
  editDialogForm.value = JSON.parse(JSON.stringify(row));
  editDialogForm.value.MedicalPointCode =
    row.MedicalPointCode && parseInt(row.MedicalPointCode);
  editDialogForm.value.VenuesCode =
    row.VenuesCode && row.VenuesCode.split(',').map(Number);
  editDialogForm.value.reSetPassWord = false;
  console.info(row, '数据');
};

const editFormSubmit = async () => {
  console.info(editDialogForm.value, '数据修改');
  await editDialogRef.value.validate(async (valid) => {
    if (valid) {
      let editobject = toRaw(editDialogForm.value);

      if (editobject.reSetPassWord) {
        editDialogForm.value.PassWord = '343F03E00E80FE0F4239A447D2DC80FC';
      }
      editobject.MedicalPointName = MedicalPointRef.value.selectedLabel;
      editobject.VenuesCode = editDialogForm.value.VenuesCode?.toString();
      await editMedicalPerson(editobject)
        .then(() => {
          editDialogVisible.value = false;
          initData();
          // initDict();
          ElMessage.success('编辑成功');
        })
        .catch(() => {
          ElMessage.error('编辑失败');
        });
    } else {
      ElMessage.error('请按要求填写');
      return false;
    }
  });
};
//删除
const rowDelete = async (id) => {
  await ElMessageBox.confirm('确定删除该条记录吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async (action) => {
    if (action === 'confirm') {
      await delMedicalPerson({ code: id })
        .then(() => {
          initData();
          // initDict();
          ElMessage.success('删除成功');
        })
        .catch((err) => {
          console.log(err);
          ElMessage.success('删除失败');
        });
    }
  });
};
//搜索
const onSubmit = async () => {
  formSearch.page = 1;
  await initData();
};
//重置
const onReset = async () => {
  formSearch.Name = '';
  formSearch.Type = '';
  formSearch.Code = '';
  formSearch.GuaranteeType = '';
  formSearch.page = 1;
  await initData();
};
//分页
const handleCurrentChange = async (val) => {
  formSearch.page = val;
  await initData();
};
//导入Excel表格
const toLead = (e) => {
  importExcel.methods.importExcel(e, (res) => {
    if (res.length > 0) {
      addMedicalPersonList(res).then(() => {
        ElMessage.success('导入成功');
        initDict();
        initData();
      });
    } else {
      ElMessage.error('请选择文件');
    }
  });
};
</script>

<style lang="scss" scoped>
.medicalPerson {
  margin: 14px 15px 14px;
  .medicalPerson-header {
    .drug-head-contatiner-title2 {
      font-size: 18px;
      font-weight: bold;
      color: #333;
      margin-top: 14px;
    }
  }
  .medicalPerson-list {
    margin-top: 14px;
  }
}
</style>
